<template>
  <div class="music"
       ref="music">
    <audio :src="`${$publicPath}audio/zhifeiji.mp3`"
           autoplay
           id="audio" loop></audio>
    <div class="btn"
         v-for="(item,index) in numArr"
         :key="index"
         :style="{left:item.left,top:item.top}"
         @click="navTo(index)">
      <i class="iconfont">&#xe60a;</i>
    </div>
    <div class="music-round">
      <div class="music-in">
        <i class="iconfont">&#xe6af;</i>
      </div>
      <!-- <div class="boder boder-1"></div> -->
      <!-- <div class="boder boder-2"></div> -->
      <div class="boder boder-3"></div>

    </div>
  </div>
</template>
<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
@Component
export default class Music extends Vue {
  sport: Array<number> = [];
  $refs!: HTMLFormElement;
  numArr: Array<object> = [];
  audio: any;
  aBall: any;
  maxTop: number = 0;
  maxLeft: number = 0;
  clientHeight: number = document.documentElement.clientHeight;
  clientWidth: number = document.documentElement.clientWidth;
  animate: any = null;
  beforeRouteLeave(to: any, from: any, next: any) {
    cancelAnimationFrame(this.animate);
    next();
  }
  beforeRouteEnter(to: any, from: any, next: any) {
    if (["musicDetails", "mine", "index", "like"].includes(from.name)) {
      next(($vm: any) => {
        $vm.$nextTick(() => {
          cancelAnimationFrame($vm.animate);
          $vm.animate = $vm.auto();
        });
      });
    } else {
      next();
    }
  }

  mounted() {
    this.createBall();
  }
  activated() {
    this.audio = document.getElementById("audio");
    this.audio.load();
    this.audio.play();
  }
  createBall(): void {
    for (let index = 0; index < 10; index++) {
      this.numArr.push({
        left: `${Math.random() * this.clientWidth}px`,
        top: `${Math.random() * this.clientHeight}px`
      });
    }
    this.$nextTick(() => {
      //生成num个div
      this.aBall = document.querySelectorAll(".btn");
      let i = 0;
      for (const iterator of this.aBall) {
        i++;
        iterator.leftVal = iterator.topVal = i + 3;
      }
      this.maxTop = this.clientHeight - this.aBall[0].clientHeight - 20; //获取top的最大值
      this.maxLeft = this.clientWidth - this.aBall[0].clientWidth; //获取left的最大值
      this.auto();
    });
  }
  auto() {
    for (const iterator of this.aBall) {
      let startL = iterator.offsetLeft; //取每个球的初始left和TOP值
      let startT = iterator.offsetTop; //取每个球的初始left和TOP值
      let Left = startL + iterator.leftVal; //改变，每个球的left和top值
      let Top = startT + iterator.topVal; //改变，每个球的left和top值

      if (Left >= this.maxLeft || Left <= 0) {
        Left = Math.min(Left, this.maxLeft - 1); //限制Left的最大值
        Left = Math.max(Left, 1); //限制最小值
        iterator.leftVal = -iterator.leftVal;
        let size = this.randomSize();
        iterator.style.cssText = `background-color:${this.randomColor()};width:${
          size.width
        }px;height:${size.height}px`;
      }
      if (Top >= this.maxTop || Top <= 0) {
        iterator.topVal = -iterator.topVal;

        Top = Math.min(Top, this.maxTop - 1); //限制Left的最大值
        Top = Math.max(Top, 1); //限制最小值
        iterator.style.backgroundColor = this.randomColor();
      }

      iterator.style.top = Top + "px";
      iterator.style.left = Left + "px";
    }

    this.animate = requestAnimationFrame(this.auto);
  }
  randomColor() {
    let r: number = Math.floor(Math.random() * 256),
      g: number = Math.floor(Math.random() * 256),
      b: number = Math.floor(Math.random() * 256);
    return "rgb(" + r + "," + g + "," + b + ")";
  }
  randomSize() {
    let size = Math.floor(Math.random() * (30 + 1 - 50) + 50);
    return { width: size, height: size };
  }
  navTo (index:any) {
    this.$router.push({name:'musicDetails',params:{id:index}})
  }
}
</script>
<style lang="scss" scoped>
.music {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url("../../../public/img/music/bg.jpg");
  background-size: cover;
  .music-round {
    width: 3rem;
    height: 3rem;
    padding: 0.5rem;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    .music-in {
      width: 50%;
      height: 50%;
      // background-color: #b2c92f;
      animation: music 3s infinite linear;
      color: #fff;
      font-size: 0.6rem;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .boder {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      margin: auto;
      border-radius: 50%;
      &::after {
        width: 0.1rem;
        height: 0.1rem;
        display: block;
        content: "";
        border-radius: 50%;
        position: absolute;
        left: -0.05rem;
        top: 0;
        bottom: 0;
        margin: auto;
      }
      &-1 {
        width: 100%;
        height: 100%;
        border: 1px solid #fff;
        animation: border-1 2s infinite linear;
        &::after {
          background-color: #fff;
        }
      }
      &-2 {
        width: 80%;
        height: 80%;
        border: 1px solid #fff;
        animation: border-2 3s infinite linear;
        &::after {
          background-color: #fff;
        }
      }
      &-3 {
        width: 60%;
        height: 60%;
        border: 1px solid #fff;
        animation: border-3 5s infinite linear;
        &::after {
          background-color: #fff;
        }
      }
    }

    @keyframes border-1 {
      0% {
        transform: scale(1) rotate(0deg) rotateY(0deg);
        opacity: 0.8;
      }
      50% {
        transform: scale(2) rotate(180deg) rotateY(360deg);
        opacity: 0.5;
      }
      100% {
        transform: scale(1) rotate(360deg) rotateY(180deg);
        opacity: 0.8;
      }
    }
    @keyframes border-2 {
      0% {
        transform: scale(1) rotate(0deg) rotateX(0deg);
        opacity: 0.8;
      }
      50% {
        transform: scale(3) rotate(180deg) rotateX(360deg);
        opacity: 0.5;
      }
      100% {
        transform: scale(1) rotate(360deg) rotateX(180deg);
        opacity: 0.8;
      }
    }
    @keyframes border-3 {
      0% {
        transform: scale(1) rotate(0deg);
        opacity: 0.8;
      }
      50% {
        transform: scale(1.2) rotate(180deg);
        opacity: 0.5;
      }
      100% {
        transform: scale(1) rotate(360deg);
        opacity: 0.8;
      }
    }
    @keyframes music {
      0% {
        transform: rotate(0deg);
      }

      100% {
        transform: rotate(360deg);
      }
    }
  }
  .btn {
    width: 50px;
    height: 50px;
    position: absolute;
    background-color: #fff;
    opacity: 0.55;
    color: #fff;
    border-radius: 50%;
    transition: all ease 0.15s;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    animation: star 3s infinite linear;
    &::after {
      display: block;
      content: "";
      position: absolute;
      width: 120%;
      height: 120%;
      border-radius: 50%;
      animation: rounds 3s infinite linear;
      border: 1px solid #fff;
    }
    @keyframes rounds {
      0% {
        width: 120%;
        height: 120%;
        transform: rotate(0deg);
      }
      50% {
        width: 150%;
        height: 150%;
        transform: rotate(180deg);
      }
      100% {
        width: 120%;
        height: 120%;
        transform: rotate(360deg);
      }
    }
    @keyframes star {
      0% {
        transform: rotate(0deg);
      }
      50% {
        transform: rotate(180deg);
      }
      100% {
        transform: rotate(360deg);
      }
    }
  }
}
</style>